<?php 
	class Db_message extends CI_Model{
		public function __construct(){
			parent::__construct();
		}
		
		public function message_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
		}
		
		public  function datalist(){
			/* ตัวแปร แบบ array เก็บฟิวด์ใน ฐานข้อมูล ต้องตรงกัน กับ จำนวน columns ที่อยู่ในไฟล์ server_side.php ด้วย
			 */
			$aColumns = array( 't1.pkid','t1.title','t2.name','t1.type','t1.pkid','t1.pkid');
			/* กำหนด primary key ให้กับคอลัมน์ */
			$sIndexColumn = "t1.pkid";
			
			
			if($this->session->userdata("sess_customer_name") != "eshine"){
				
				$customer_pkid = $this->session->userdata("sess_customer_id");
				$sTable = "mobile_message t1 LEFT JOIN mobile_customer t2 ON t1.customer_pkid = t2.pkid WHERE t1.customer_pkid =".$customer_pkid."";
			}else{
				$sTable = "mobile_message t1 LEFT JOIN mobile_customer t2 ON t1.customer_pkid = t2.pkid";
			}
			/* ชื่อตาราง */
	
			/*
			 * แบ่งหน้า
			*/
			$sLimit = "";
			if ($this->input->post('iDisplayStart') != ""  && ($this->input->post('iDisplayLength') != '-1' ))
			{
				$sLimit = "LIMIT ".$this->input->post('iDisplayStart') .", ".$this->input->post('iDisplayLength') ;
			}	
			/*
			 * จัดเรียงลำดับ
			*/
			$sOrder = "";
			if ($this->input->post('iSortCol_0') != "" )
			{
				$sOrder = "ORDER BY  ";
				for ( $i=0 ; $i<intval( $this->input->post('iSortingCols')) ; $i++ )
				{
					if ($this->input->post('bSortable_'.intval($this->input->post('iSortCol_'.$i)) ) == "true" )
					{
						$sOrder .= $aColumns[ intval( $this->input->post('iSortCol_'.$i) ) ]."
						".$this->input->post('sSortDir_'.$i) .", ";
					}
				}
					
				$sOrder = substr_replace( $sOrder, "", -2 );
				if ( $sOrder == "ORDER BY" )
				{
					$sOrder = "";
				}
			}
		
		
			$sWhere = "";
			if ($this->input->post('sSearch') != "" && $this->input->post('sSearch') != "" )
			{
				$sWhere = "WHERE (";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$sWhere .= $aColumns[$i]." LIKE '%". $this->input->post('sSearch') ."%' OR ";
				}
				$sWhere = substr_replace( $sWhere, "", -3 );
				$sWhere .= ')';
			}
		
			/* เวลาค้นหาในคอลัมน์  */
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				if ( $this->input->post('bSearchable_'.$i) != "" && $this->input->post('bSearchable_'.$i) == "true" && $this->input->post('sSearch_'.$i) != '' )
				{
					if ( $sWhere == "" )
					{
						$sWhere = "WHERE ";
					}
					else
					{
						$sWhere .= " AND ";
					}
					$sWhere .= $aColumns[$i]." LIKE '%".$this->input->post('sSearch_'.$i)."%' ";
				}
			}
		
		
			/*
			 *คำสั่ง SQL ดึงข้อมูลออกมาโชว์
			*
			*/
			$sQuery = "
			SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
			FROM   $sTable
			$sWhere
			$sOrder
			$sLimit
			";
			$rResult = $this->db->query( $sQuery);
		
			/* จำนวนข้อมูลหลังจากค้นหา */
			 $sQuery = "
			 SELECT FOUND_ROWS() as numT
			 ";
			 $rResultFilterTotal = $this->db->query($sQuery) ;
			 $aResultFilterTotal = $rResultFilterTotal->row();
			 $iFilteredTotal = $aResultFilterTotal->numT;
		
			 /*  จำนวนทั้งหมด */
			$sQuery = "
			SELECT COUNT(".$sIndexColumn.") as num
					FROM   $sTable
					";
					$rResultTotal = $this->db->query( $sQuery);
					$aResultTotal = $rResultTotal->row();
					$iTotal = $aResultTotal->num;
		
		
					/*
					* ส่วนการแสดงผล
			*/
			$output = array(
			 "sEcho" => intval($this->input->post('sEcho')),
			 		"iTotalRecords" => $iTotal,
			 		"iTotalDisplayRecords" => $iFilteredTotal,
			 		"aaData" => array()
			 );
		
		
		
			 		foreach ($rResult->result_array()  as $aRow)  //loop ออกมาแสดงข้อมูล
			 		{
			 			$row = array();
			 			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			 			{
			 			if ( $aColumns[$i] == "version" )
			 			{
			 				$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
			 					
			 				}
			 				else if ( $aColumns[$i] != ' ' )
			 				{
			 				if($i == 0){
			 				$row[] = '<input type="checkbox" name="check[]" value="'.$aRow['pkid'].'" />';
			 				}else if($i == 1){
			 					$row[] = $aRow['title'];
			 				}else if($i == 2){
								$this->db->where("message_pkid",$aRow['pkid']);
								$num = $this->db->count_all_results("mobile_mes_natificate");
				 				 $row[] = $aRow['name'].'
								<a href="'.base_url().'manage_message/message_view/'.$aRow['pkid'].'">Subcate['.$num.']</a>';
			 		
							}else if($i == 3){
			 					if($aRow['type']==1)
			 						$row[] = "<b>New/Activities</b>";
								elseif($aRow['type']==2)
									$row[] = "<b>Events</b>";
								elseif($aRow['type']==3)
									$row[] = "<b>Promotion</b>";
			 				}
							else if($i == 4){
				 				$row[] = '
				 				<a href="'.base_url().'manage_message/message_item_del/'.$aRow['pkid'].'" >
				 				<img border="0" src="'.base_url().'images/delete-icon.png">
				 				</a>';
			 				}
			 					
			 				}
			 			}
			 			$output['aaData'][] = $row;
			 			}
			 			return $output;
			 			//	echo json_encode( $output );
			 		}
					
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
					
				
				
		
		
		
		
		
		
		public function type($L =""){
			$L1="";$L2="";$L3="";
			if($L == 1){
				$L1 = 'selected="selected"';
			}else if($L == 2){
				$L2 = 'selected="selected"';
			}else if($L == 3){
				$L3 = 'selected="selected"';
			}
			$str =  '<option value="">=select type Message=</option>"';
			$str .= '<option value="1" '.$L1.'>New/Activeties</option>"';
			$str .= '<option value="2" '.$L2.'>Events</option>"';
			$str .= '<option value="3" '.$L3.'>Promotion</option>"';
			return $str;
		}
	}
?>
